package com.zhiyou.controller.ContractController;

import com.github.pagehelper.PageInfo;
import com.zhiyou.bean.Contract;
import com.zhiyou.bean.Lessee;
import com.zhiyou.service.ContractService.ContractServiec;
import com.zhiyou.service.HouseService.HouseService;


import com.zhiyou.service.SystemLesseeService.LesseeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/contract")
public class ContractController {

    @Autowired
    ContractServiec contractServiec;
    @Autowired
    LesseeService lesseeService;
    @Autowired
    HouseService houseService;

    @RequestMapping("/msg")
    public String getContractMsg(@RequestParam(value = "pn",defaultValue = "1") Integer pn,Map<String,PageInfo> map,HttpSession session){
        PageInfo contractMsg = contractServiec.getContractMsg(pn,(Contract) session.getAttribute("selKeyword"));
        map.put("contractMsg",contractMsg);
        return "contractlist";
    }

    @RequestMapping("/detail")
    public String getContractDetailMsg(Integer cid, Map<String, Contract> map){
        Contract con = new Contract();
        con.setcId(cid);
        map.put("contract",contractServiec.getContractDetailMsg(con));
        return "contractdetail";
    }

    @RequestMapping("/delete")
    public String deleteContract(String cid,HttpSession session){
        int isOK = contractServiec.deleteContract(Integer.valueOf(cid));
        String msg = isOK == 1 ?  "删除成功":"删除失败";
        session.setAttribute("deleteMsg",msg);
        return "redirect:/contract/msg";
    }

    @RequestMapping("/update")
    public String LodeUpdateContract(Integer cid,Map<String,Object> map){
        Contract con = new Contract();
        con.setcId(cid);
        Contract msg = contractServiec.getContractDetailMsg(con);
        List<Lessee> lesseeMsg = lesseeService.getAll();
        map.put("contract",msg);
        map.put("lessee",lesseeMsg);
        return "contractedit";
    }

    @RequestMapping("/save")
    public String doUpdateContract(Contract contract,String overtime, HttpSession session){
        int isOk = contractServiec.updateContract(contract,overtime);
        String msg = isOk == 1 ? "更新成功":"更新失败";
        session.setAttribute("updateMsg",msg);
        return "redirect:/contract/msg";
    }
    @RequestMapping("/jumpToAdd")
    public String jumpToAdd(){
        return "contractadd";
    }

    @RequestMapping("/doAdd")
    public String doAddContract(Contract contract,String onTime,String startTime,String overTime,HttpSession session){
        int isOk = contractServiec.addContract(contract, onTime, startTime, overTime);
        String msg = isOk == 1 ? "新增成功":"新增失败";
        session.setAttribute("addMsg",msg);
        return "redirect:/contract/msg";
    }
    @RequestMapping("/search")
    public String searchContract(Contract contract,HttpSession session){
        if(contract.getcId() != null || contract.gethId() != null || contract.getlId() != null){
            session.setAttribute("selKeyword",contract);
        }else{
            session.removeAttribute("selKeyword");
        }

        return "redirect:/contract/msg";
    }

//    @RequestMapping("/exportExcel")
//    public void exportExcel(HttpServletResponse response, Model model){
//        List<Contract> emps = contractServiec.exportExcel();
//        //新建一个Excel 对象
//        //把weorkbook对象  理解为一个 excel
//        Workbook workbook=new XSSFWorkbook();
//        //在excel中创建一个 sheet
//        Sheet sheet=workbook.createSheet("本月");
//        String[] titles={"合同编号","房子编号","租户编号","合同签订时间","合同生效时间","合同失效时间","合同总金额","合同支付方式"};
//        //创建一行
//        Row row=sheet.createRow(0);
//        for (int i = 0; i <titles.length ; i++) {
//            //cell 是一个单元格
//            Cell cell=row.createCell(i);
//            //往单元格里面放数据
//            cell.setCellValue(titles[i]);
//        }
//        for (int i = 0; i <emps.size() ; i++) {
//            row=sheet.createRow(i+1);
//            Contract employee=emps.get(i);
//            Cell empnoCell=row.createCell(0);
//            empnoCell.setCellValue(employee.getcId());
//            Cell nameCell=row.createCell(1);
//            nameCell.setCellValue(employee.gethId());
//            Cell sexCell=row.createCell(2);
//            sexCell.setCellValue(employee.getlId());
//            Cell ageCell=row.createCell(3);
//            ageCell.setCellValue(employee.getcTime().toString());
//            Cell jobCell=row.createCell(4);
//            jobCell.setCellValue(employee.getcTaketime().toString());
//            Cell departidCell=row.createCell(5);
//            departidCell.setCellValue(employee.getcOvertime().toString());
//            Cell moneyCell=row.createCell(6);
//            moneyCell.setCellValue(employee.getcRental());
//            Cell cashCell=row.createCell(7);
//            cashCell.setCellValue(employee.getcCash());
//
//        }
//        try {
//            String fileName= URLEncoder.encode("本月合同.xlsx","UTF-8");
//            response.setContentType("application/octet-stream");
//            response.setHeader("content-disposition","attachment;filename="+fileName);
//            response.setHeader("filename",fileName);
//            //把workbook对象 写出去，到 response的输出流
//            workbook.write(response.getOutputStream());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

}
